﻿/* Writer:白晓旭 2012/4/23 */
$(function () {
    /*是否有孩子*/
    //ShowChlidBir();
    //$("input[name=Children]").click(function () {
    //    ShowChlidBir();
    //});
    /*表单验证*/
    $.formValidator.initConfig();
    $("#nickinp").focus(function () { $(this).parent("dd").removeClass("right"); });
    $("#nickinp").formValidator({ onfocus: "2-16个字符，可由中英文、数字组成" }).InputValidator({ min: 2, max: 16, onerror: "昵称格式错误" }).RegexValidator({ regexp: "username", datatype: "enum", onerror: "昵称格式错误" })
	    .DefaultPassed();
    //$("#realinp").formValidator({ empty: true, onfocus: "2-10个汉字或4-20个英文字母", oncorrect: "" }).InputValidator({ min: 2, max: 20, onerror: "请输入真实姓名，2-10个汉字或4-20个英文" }).RegexValidator({ regexp: "chineseOrLetter", datatype: "enum", onerror: "请输入真实姓名，2-10个汉字或4-20个英文" });
    //$("#realinp").InputValidator({ min: 2, max: 20, onerror: "请输入真实姓名，2-10个汉字或4-20个英文" }).RegexValidator({ regexp: "chineseOrLetterOrChar", datatype: "enum", onerror: "请输入真实姓名，2-10个汉字或4-20个英文" });
    $("#sexRadio").formValidator({ onshow: "", onfocus: "", oncorrect: "" }).InputValidator({ min: 1, max: 1, onerror: "请选择一个性别" });
    //$("#cardinp").formValidator({ empty: true, onshow: "", onfocus: "请填写正确的身份证号码", oncorrect: "" }).FunctionValidator({ fun: isCardID });
    //$("#qqinp").formValidator({ empty: true, onshow: "", onfocus: "5-11位QQ号码", oncorrect: "" }).RegexValidator({ regexp: "^\\d{5,11}$", onerror: "请填写正确的QQ号码" });
    //$("#msninp").formValidator({ empty: true, onshow: "", onfocus: "请填写正确的MSN账号", oncorrect: "" }).RegexValidator({ regexp: "^([\\w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([\\w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", onerror: "请填写正确的MSN账号" });

    /*提交按钮可用*/
    jQuery("#sumBgn").attr("disabled", false);
    jQuery("#sumBgn").text("保存");

});
/*加载地域*/
function loadArea(pid, controlId) {

}
/*展示孩子生日下拉*/
//var ShowChlidBir = function () {
//    var val = $("input[name=Children]:checked").val();
//    if (val == 1) {
//        $("#chlidbirth").show();
//    } else {
//        $("#chlidbirth").hide();
//    }
//};
/*提交表单(表单key如下)*/
/* 
NickName RealName Sex birthY  birthM birthD Marriage Children CbirthY CbirthM CbirthD Education 
Profession MonthInCome PHometown CHometown PNowArea CNowArea DetailAddress IdentityCard QQ MSN HomePage
*/
var myinfoformsubmit = function () {
    var isPass = jQuery.formValidator.PageIsValid('1');
    if (isPass) {
        var nickName = HTMLEnCode($("input[name=NickName]").val());
        var realName = HTMLEnCode($("input[name=RealName]").val());
        var sex = $("input[name=Sex]:checked").val();
        //var birthday = FnGetSelectValue("birthY") + "-" + FnGetSelectValue("birthM") + "-" + FnGetSelectValue("birthD");
        var birthday = getBirthday();
        var marriage = $("select[name=Marriage] option:selected").val();
        //var children = $("input[name=Children]:checked").val();
        //var childrenBirthday = "";
        //if (children > 0)/*有孩子*/
        //{
        //    childrenBirthday = FnGetSelectValue("CbirthY") + "-" + FnGetSelectValue("CbirthM") + "-" + FnGetSelectValue("CbirthD");
        //}
        var education = $("select[name=Education] option:selected").val();
        var profession = $("select[name=Profession] option:selected").val();
        var monthInCome = $("select[name=MonthInCome] option:selected").val();
        var hometown = jQuery("select[name=PHometown] option:selected").val() + '-' +
                                jQuery("select[name=CHometown] option:selected").val();
        var nowArea = jQuery("select[name=PNowArea] option:selected").val() + '-' +
                                jQuery("select[name=CNowArea] option:selected").val();
        //var detailAddress = HTMLEnCode($("input[name=DetailAddress]").val());
        var identityCard = HTMLEnCode($("input[name=IdentityCard]").val());
        //var qq = HTMLEnCode($("input[name=QQ]").val());
        //var msn = HTMLEnCode($("input[name=MSN]").val());
        //var homePage = $("input[name=HomePage]").val();
        var token = $("input[name=posttoken]").val();
        var obj = $("#sumBgn");
        obj.attr("disabled", true); obj.text("请稍后..");
        $.ajax({
            timeout: 20000,
            type: "POST",
            async: true,
            url: "saveUserInfo",
            data: { nickName: nickName, realName: realName, sex: sex, birthDay: birthday,
            	marriageStatus: marriage,
                //Children: '0', ChildrenBirthday: '',
            	educationLevel: education, industry: profession, monthIncome: monthInCome,
                Hometown: hometown, NowArea: nowArea,
                //DetailAddress: '', QQ: '',MSN: '', HomePage: '',
                identityCard: identityCard, Token: token
            },
            cache: false,
            dataType : "json",
            error: function () {
                alert("提交失败，请重试");
                obj.attr("disabled", false); obj.text("保存");
            },
            success: function (data) {
            		var success = data.success;
                if (success == true) {
                    alert("提交成功");
                    window.location.reload();
                } else {
                    alert(data.msg);
                    window.location.reload();
                }
                obj.attr("disabled", false); obj.text("保存");
            }
        });
    }
};

function getBirthday() {
    var y = FnGetSelectValue("birthY");
    var m = FnGetSelectValue("birthM");
    var d = FnGetSelectValue("birthD");

    if (y == "0" || m == "0" || d == "0") return "0-0-0";
            
    return FnGetSelectValue("birthY") + "-" + FnGetSelectValue("birthM") + "-" + FnGetSelectValue("birthD");
    }

function HTMLEnCode(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&gt;");
    s = s.replace(/</g, "");
    s = s.replace(/>/g, "");
    s = s.replace(/ /g, "");
    s = s.replace(/\"/g, "");
    s = s.replace(/\'/g, "");
    s = s.replace(/\n/g, "");
    s = s.replace(/\//g, "");
    s = s.replace(/\(/g, "");
    s = s.replace(/\)/g, "");
    s = s.replace(/\=/g, "");
    return s;
}